//老闭做的
package cn.lanqiao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import cn.lanqiao.dao.UserDao;
import cn.lanqiao.entiy.User;
import cn.lanqiao.util.JsfUtil;

public class UserDaoImpl implements UserDao{
	
	//添加学员账号
	@Override
	public int add(User user) {
		Connection conn = JsfUtil.getConnection();
		PreparedStatement ps =null;
		String sql="insert into J_User (u_id,uuser,upassword) values (?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getU_id());
			ps.setString(2, user.getUuser());
			ps.setString(3, user.getUpassword());
			int affter= ps.executeUpdate();
			if (affter>0) {
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JsfUtil.close(null, ps, conn);
		}
		return 0;
	}
	//修改学员账号
	@Override
	public int update(User user) {
		Connection conn = JsfUtil.getConnection();
		PreparedStatement ps =null;
		String sql="update J_User set upassword=? where uuser=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUpassword());
			ps.setString(2, user.getUuser());
			int affter= ps.executeUpdate();
			if (affter>0) {
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JsfUtil.close(null, ps, conn);
		}
		return 0;
	}
	//获取学员账号
	@Override
	public User search(String uuser) {
		Connection conn = JsfUtil.getConnection();
		PreparedStatement ps =null;
		ResultSet rs = null;
		String sql="select * from J_USER where uuser=?";
		User user = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, uuser);
			rs= ps.executeQuery();
			if(rs.next()) { //创建游标
				user = new User(rs.getString("u_id"), rs.getString("uuser"), rs.getString("upassword"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JsfUtil.close(rs, ps, conn);
		}
		return user;
	}


}
